'use strict';

const Controller = require('egg').Controller;
class ReservationController extends Controller {

  /**
   * 保存预定结果
   */
  async saveReservation() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};

    try {
      const reservation = await this.ctx.service.reservationService.add(ctx.body);

      retrunMap = { code: '1', msg: '成功', data: reservation };

    } catch (err) {
      retrunMap = { code: '0', msg: '失败' };
      console.log(err);
    }

    ctx.body = retrunMap;
  }

  /**
   * 获取要上门订单
   */
  async getMessages() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};

    try {
      const list = await this.ctx.service.reservationService.getMessages(ctx.body);

      retrunMap = { code: '1', msg: '成功', data: list };

    } catch (err) {
      retrunMap = { code: '0', msg: '失败' };
      console.log(err);
    }

    ctx.body = retrunMap;
  }

  /**
   * 获取要子女要收货的订单
   */
  async getMessagesByShouhuo() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};

    try {
      const list = await this.ctx.service.reservationService.getMessagesByShouhuo(ctx.body);

      retrunMap = { code: '1', msg: '成功', data: list };

    } catch (err) {
      retrunMap = { code: '0', msg: '失败' };
      console.log(err);
    }

    ctx.body = retrunMap;
  }

  /**
   * 保存订单处理结果
   */
  async saveResult() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};

    try {
      const list = await this.ctx.service.reservationService.saveResult(ctx.body);

      retrunMap = { code: '1', msg: '成功', data: list };

    } catch (err) {
      retrunMap = { code: '0', msg: '失败' };
      console.log(err);
    }

    ctx.body = retrunMap;
  }

  /**
   * 通订单号获取详细
   */
  async getOrderDetail() {
    const { ctx } = this;
    const my_order = ctx.params.orderId;
    let retrunMap = {};

    try {
      const list = await this.ctx.service.reservationService.getOrderDetail(my_order);

      retrunMap = { code: '1', msg: '成功', data: list };

    } catch (err) {
      retrunMap = { code: '0', msg: '失败' };
      console.log(err);
    }

    ctx.body = retrunMap;
  }

}

module.exports = ReservationController;
